package com.snail.pattern.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyFactory {

    private Object target;

    public ProxyFactory(Object target){
        this.target = target;
    }

    public Object getProxyInstance(){
        return Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if (method.getName().equals("coding")){
                            System.out.println("coder v ready...");
                            Object result = method.invoke(target,args);
                            System.out.println("coder v done...");
                            return result;
                        }else{
                            return method.invoke(target,args);
                        }
                    }
                });
    }

}
